<?php
require_once "common.php";
if(GZIP){
	ob_start('compress');
}
#var_dump($_POST);
header("Expires: Mon, 26 Jul 1970 05:00:00 GMT");
header("Last-Modified: ".date("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");  
header("Pragma: no-cache");

echo "/***** kwestion start *****/";

$_POST = $_GET['p'];

if(isset($_POST['process']) && $_POST['process'] == 'timeline'){
	$action = $_POST['action'];
	$user = $_POST['user'];
	$listtmp=explode('/',$user);
	if (count($listtmp)<2){
		$listtmp = array('','');
	}
	$actions = array('home'=>API_URL.'/statuses/home_timeline.json?include_rts=1&',
		'user'=>API_URL.'/statuses/user_timeline.json?screen_name='.$user.'&',
		'following'=>API_URL."/friends/list.json?cursor=-1&screen_name={$user}&",
		'follower'=>API_URL."/followers/list.json?cursor=-1&screen_name={$user}&", //API_URL.'/statuses/user_timeline.json?screen_name='.$_POST['user'].'&',
		'dm'=>API_URL.'/direct_messages.json?',
		'fav'=>API_URL.'/favorites/list.json?screen_name='.$user.'&',
		'reply'=>API_URL.'/statuses/mentions_timeline.json?include_rts=1&',
		'rtbyme'=>API_URL."/statuses/retweeted_by_me.json?",
		'rttome'=>API_URL."/statuses/retweeted_to_me.json?",   
		'rtofme'=>API_URL."/statuses/retweets_of_me.json?",
		'listmembership'=>API_URL."/lists/memberships.json?screen_name={$user}&",
		'userlist'=>API_URL."/lists/list.json?screen_name={$user}&",
		'list'=>API_URL."/lists/statuses.json?slug={$listtmp[1]}&owner_screen_name={$listtmp[0]}&",
		'hash'=>API_URL.'/search/tweets.json?q='.urlencode($_POST['user']).'&'
	);

	$count = (isset($_POST['isfocus']) && $_POST['isfocus'])?20:1;
	if(isset($_POST['cursor']) && !isset($_POST['page'])){
		$page = '&'.((isset($_POST['isfocus']) && $_POST['isfocus'])?$_POST['cursor']:'cursor=-1');
	}
	else{
		$page = '';//'&page='.((isset($_POST['isfocus']) && $_POST['isfocus'])?$_POST['page']:1);
	}

	$since = ''; $maxid = '';
	if(isset($_POST['maxid'])){
		$since = '&max_id='.$_POST['maxid'];
	}
	if(isset($_POST['sinceid'])){
		$since = '&since_id='.$_POST['sinceid'];
	}

	$aa = $actions[$action];
	if($action!='userlist'){
		$aa.='count='.$count.$page.$since.$maxid;
	}

	$xx = twitter_process($aa);

	$action_cursor 	= array(
		'follower'=>array('users'),
		'following'=>array('users'),
		'listmembership'=>array('lists')
	);

	$action_raw = array('userlist'=>true);

	if($action=='hash'){
		//echo $aa;
		//var_dump($xx);
		$xx = $xx->statuses;
	}
	//var_dump($xx);
	if(!isset($action_cursor[$action]) && !isset($action_raw[$action])){
		$xx = processstatuses($xx);
	}

	echo json_encode($xx);
}

if(isset($_POST['process']) && $_POST['process'] == 'show'){
	$request = API_URL.'/users/show.json?screen_name='.$_POST['user'];
	echo json_encode(twitter_process($request));
}

if(isset($_POST['sendmessage'])){
	$status = stripslashes($_POST['msg']);
	if ($status) {
		$request = API_URL.'/statuses/update.json';
		$post_data = array(
			'source' => 'kwestion',
			'status' => $status);

		if(isset($_COOKIE['stopgeoinfo']) && $_COOKIE['stopgeoinfo']!='stop' && isset($_POST['lat']) && $_POST['lat'] && $_POST['lat']!='null'){
			$post_data['lat'] =  $_POST['lat'];
			$post_data['long'] =  $_POST['long'];
		}

		if(isset($_POST['inreplyto']))
			$in_reply_to = (string) $_POST['inreplyto'];
		if (is_numeric($in_reply_to)) {
			$post_data['in_reply_to_status_id'] = $in_reply_to;
		}
		$tmp = twitter_process($request, $post_data);
		echo json_encode($tmp);
	}
}

if(isset($_POST['rt'])){
	$id = $_POST['rt'];
	$request = API_URL."/statuses/retweet/".$id.".json";
	//echo $request;
	$tmp = twitter_process($request, true);
	$result = array();
	$result['code'] = !($tmp===false);
	echo json_encode($result);
}

if(isset($_POST['del_message'])){
	$id = $_POST['del_message'];
	$request = API_URL."/statuses/destroy/{$id}.json";
	$tmp = twitter_process($request,true);
	$result = array();
	$result['code'] = !($tmp===false);
	echo json_encode($result);
}

if(isset($_POST['delmsg'])){
	$id = $_POST['delmsg'];
	$request = API_URL."/direct_messages/destroy/{$id}.json";
	$tmp = twitter_process($request, true);
	$result = array();
	$result['code'] = !($tmp===false);
	$result['raw'] = $tmp;
	echo json_encode($result);
}

if(isset($_POST['addfav'])){
	$id = $_POST['addfav'];
	$request = API_URL."/favorites/create.json";
	$post_data = array('id' => $id);
	$tmp = twitter_process($request, $post_data);
	$result = array();
	$result['code'] = !($tmp===false);
	echo json_encode($result);
}

if(isset($_POST['cancelfav'])){
	$id = $_POST['cancelfav'];
	$request = API_URL."/favorites/destroy.json";
	$post_data = array('id' => $id);
	$tmp = twitter_process($request, $post_data);
	$result = array();
	$result['code'] = !($tmp===false);
	echo json_encode($result);
}

if(isset($_POST['addfollow'])){
	$user = $_POST['addfollow'];
	$request = API_URL."/friendships/create.json";
	$post_data = array('screen_name'=>$user);
	$tmp = twitter_process($request, $post_data);

	$result = array();
	$result['code'] = !($tmp===false) && (!isset($tmp->error));
	echo json_encode($result);
}

if(isset($_POST['cancelfollow'])){
	$u = $_POST['cancelfollow'];
	$request = API_URL."/friendships/destroy.json";
	$post_data = array('screen_name'=>$u);
	$tmp = twitter_process($request, $post_data);

	$result = array();
	$result['code'] = !($tmp===false) && (!isset($tmp->error));
	echo json_encode($result);
}

if(isset($_POST['showthread'])){
	$id = $_POST['showthread'];

	$result = array();
	$result['code'] = true;
	$result['search'] = array();

	for($i = 0; $i < 3; $i++){
		$raw = twitter_process(API_URL."/statuses/show/{$id}.json");
		if($raw!==false && $raw && $raw->user){
			$raw->from=$raw->user;
			array_unshift($result['search'],$raw);
			$id = $raw->in_reply_to_status_id_str;
		}
		if(!$id || $id == 0 || $id == ''){
			break;
		}
	}
	if(count($result['search'])){
		$result['search'][0]->bold = true;
	}
	echo json_encode($result);
}

if(isset($_POST['shorturl'])){
	if(isLogin()){
		$str = $_POST['shorturl'];
		$raw = processcurl('http://www.unshorten.net/api.php?r='.$str.'&t=json');
		echo $raw;
	}
}

if(isset($_POST['geo'])){
	$str = "http://maps.google.co.uk/m?q={$_POST['lat']},{$_POST['long']}";
	//echo $str;
	$raw = processcurl($str);
	//echo $raw;
	$s = '/src="(http:\/\/maps.google.com\/.*?)".*?>/i';
	preg_match_all($s, $raw,$a);

	$result = array();
	$result['code'] = true;
	$result['map'] = $a[1][0];
	echo json_encode($result);
}

if(isset($_POST['description'])){
	$description = $_POST['description'];
	$post_data = array('description'=>$description);
	$request = API_URL."/account/update_profile.json";
	$tmp = twitter_process($request, $post_data);
	$result = array();
	$result['code'] = !($tmp===false);
	$result['user'] = $tmp;
	echo json_encode($result);
}

if(isset($_POST['process']) && $_POST['process'] == 'v'){
	$request = API_URL."/account/verify_credentials.json";
	$tmp = twitter_process($request);
	echo json_encode($tmp);
}

?>/***** kwestion end *****/

